package com.ruoyi.client.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class CloudDemo {

    public static void main (String[] args) {
        CloudDemo cloudDemo = new CloudDemo();

        Map params = new HashMap();
        params.put("secret_key", "***");
        params.put("secret_code", "ddffadd3df4b4c0d8d6f9942c7a8c990");
        params.put("secret_sign", "md5(***)");
        params.put("com", "");
        params.put("recManName", "");
        params.put("recManMobile", "");
        params.put("recManPrintAddr", "");
        params.put("sendManName", "");
        params.put("sendManMobile", "");
        params.put("sendManPrintAddr", "");
        params.put("cargo", "");
        params.put("weight", "");
        params.put("remark", "");
        params.put("salt", "");
        params.put("callBackUrl", "");
        params.put("payment", "");
        params.put("dayType", "");
        params.put("pickupStartTime", "");
        params.put("pickupEndTime", "");
        params.put("expType", "");

        String post = cloudDemo.post(params);
        System.out.println("post = " + post);
    }

    public String post(Map params) {
        StringBuilder response = new StringBuilder("");
        BufferedReader reader = null;
        try {
            StringBuilder builder = new StringBuilder();
            params.forEach((key,value) ->{
                if (builder.length() > 0) {
                    builder.append('&');
                }
                try {
                    builder.append(URLEncoder.encode((String)key, "UTF-8"));
                    builder.append('=');
                    builder.append(URLEncoder.encode(String.valueOf(value), "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            });

            byte[] bytes = builder.toString().getBytes("UTF-8");
            URL url = new URL("http://cloud.kuaidi100.com/api");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", String.valueOf(bytes.length));
            conn.setDoOutput(true);
            conn.getOutputStream().write(bytes);
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line = "";
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != reader) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return response.toString();
    }
}

